package com.xyy.ec.pop.api.kingdinggmp.helpers;

import com.alibaba.fastjson.JSON;
import com.xyy.ec.pop.api.kingdinggmp.dto.erp.ErpOutOrderDto;
import com.xyy.ec.pop.api.kingdinggmp.dto.erp.ErpSkuInfoDto;
import com.xyy.ec.pop.api.kingdinggmp.dto.erp.GmpCustomerDto;
import com.xyy.ec.pop.api.kingdinggmp.dto.erp.GmpStockDto;
import com.xyy.ec.pop.api.kingdinggmp.dto.pop.*;
import com.xyy.ec.pop.api.kingdinggmp.utils.DateUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class PopHelper {
    public static List<PopErpSkuStockV2Dto> erpToPopStockConvert(List<GmpStockDto> gmpStockDtoList) {
        List<PopErpSkuStockV2Dto> dtoList = new ArrayList<>();
        Map<String, List<GmpStockDto>> collect = gmpStockDtoList.stream().collect(Collectors.groupingBy(GmpStockDto::getFNumber));
        for (Map.Entry<String, List<GmpStockDto>> entry : collect.entrySet()) {
            dtoList.add(getStock(entry.getKey(), entry.getValue()));
        }
        return dtoList;
    }

    private static PopErpSkuStockV2Dto getStock(String key, List<GmpStockDto> value) {
        PopErpSkuStockV2Dto erpSkuStockV2Dto = new PopErpSkuStockV2Dto();
        erpSkuStockV2Dto.setErpCode(key);
        // 最老生产日期
        String oldestProDate = value.stream().map(GmpStockDto::getFPRODUCEDATE).filter(Objects::nonNull).min(String::compareTo).orElse(null);
        // 最新生产日期
        String newProDate = value.stream().map(GmpStockDto::getFPRODUCEDATE).filter(Objects::nonNull).max(String::compareTo).orElse(null);
        // 近效期至
        String nearEffect = value.stream().map(GmpStockDto::getFEXPIRYDATE).filter(Objects::nonNull).min(String::compareTo).orElse(null);
        // 远效期至
        String farEffect = value.stream().map(GmpStockDto::getFEXPIRYDATE).filter(Objects::nonNull).max(String::compareTo).orElse(null);
        Integer availableQyt = value.stream().map(GmpStockDto::getF_RPIHInvStockQty).filter(Objects::nonNull).reduce(0, Integer::sum);
        erpSkuStockV2Dto.setOldestProDate(DateUtils.convertStringToDate(oldestProDate));
        erpSkuStockV2Dto.setNewProDate(DateUtils.convertStringToDate(newProDate));
        erpSkuStockV2Dto.setNearEffect(DateUtils.convertStringToDate(nearEffect));
        erpSkuStockV2Dto.setFarEffect(DateUtils.convertStringToDate(farEffect));
        erpSkuStockV2Dto.setAvailableQyt(availableQyt);
        return erpSkuStockV2Dto;
    }

    public static List<PopErpClientInfoDto> erpToPopCustomerConvert(List<GmpCustomerDto> gmpCustomerDtoList) {

        return gmpCustomerDtoList.stream().map(m -> {
            PopErpClientInfoDto popErpClientInfoDto = new PopErpClientInfoDto();
            popErpClientInfoDto.setCustomerId(m.getFCUSTID());
            popErpClientInfoDto.setCustomerCode(m.getFNumber());
            popErpClientInfoDto.setCustomerName(m.getFName());
            return popErpClientInfoDto;
        }).collect(Collectors.toList());
    }

    public static List<PopErpSkuForInsertDto> erpToPopSkuInfoConvert(List<ErpSkuInfoDto> erpSkuInfoDtos) {
        return erpSkuInfoDtos.stream().map(m -> {
                    PopErpSkuForInsertDto popErpSkuForInsertDto = new PopErpSkuForInsertDto();
                    popErpSkuForInsertDto.setErpCode(m.getFNumber());
                    popErpSkuForInsertDto.setProductName(m.getFName());
                    popErpSkuForInsertDto.setCommonName(m.getFName());
                    popErpSkuForInsertDto.setApprovalNumber("-");
                    popErpSkuForInsertDto.setCode("0");
                    popErpSkuForInsertDto.setManufacturer("河北万东药业有限公司");
                    popErpSkuForInsertDto.setMediumPackageNum(1);
                    popErpSkuForInsertDto.setProductUnit(m.getFBaseUnitId_Fname());
                    popErpSkuForInsertDto.setSpec(m.getF_RPIH_bzgg());
                    popErpSkuForInsertDto.setDosageForm(m.getF_RPIH_jx() == "" ? "中药" : m.getF_RPIH_jx());
                    popErpSkuForInsertDto.setTerm(m.getFExpPeriod());
                    popErpSkuForInsertDto.setSuggestPrice(BigDecimal.valueOf(9999));
                    popErpSkuForInsertDto.setProductionArea(m.getF_RPIH_cd());
                    popErpSkuForInsertDto.setAvailableQyt(1);
                    popErpSkuForInsertDto.setMonomerPrice(BigDecimal.valueOf(9999));
                    popErpSkuForInsertDto.setBasePrice(BigDecimal.valueOf(9999));
                    popErpSkuForInsertDto.setChainPrice(BigDecimal.valueOf(9999));
                    popErpSkuForInsertDto.setOldestProDate(DateUtils.now());
                    popErpSkuForInsertDto.setNewProDate(DateUtils.now());
                    popErpSkuForInsertDto.setNearEffect(DateUtils.addTimeMonth(12));
                    popErpSkuForInsertDto.setFarEffect(DateUtils.addTimeMonth(12));
                    popErpSkuForInsertDto.setMarketAuthor("河北万东药业有限公司");
                    popErpSkuForInsertDto.setAliasName("");
                    popErpSkuForInsertDto.setDrugClassification("");
                    popErpSkuForInsertDto.setManufacturingLicenseNo("");
                    popErpSkuForInsertDto.setMarketAuthorAddress("");
                    popErpSkuForInsertDto.setManufacturerAddress("");
                    popErpSkuForInsertDto.setFilingsAuthor("");
                    return popErpSkuForInsertDto;
                }

        ).collect(Collectors.toList());

    }

    public static List<SellerOrderDeliverDetailForErpDto> erpToPopOutOrder(List<ErpOutOrderDto> erpOutOrderDtos) {
        List<SellerOrderDeliverDetailForErpDto> popOutOrderDtos = new ArrayList<>();
        for (ErpOutOrderDto erpOutOrderDto : erpOutOrderDtos) {
            SellerOrderDeliverDetailForErpDto popOutOrder = new SellerOrderDeliverDetailForErpDto();
            popOutOrder.setOrderNo(erpOutOrderDto.getF_RPIH_SourceBillNo());
            popOutOrder.setOrderDetailId(Long.valueOf(erpOutOrderDto.getFEntryNote()));
            popOutOrder.setProductName(erpOutOrderDto.getFMaterialName());
            popOutOrder.setErpCode(erpOutOrderDto.getFMaterialId());
            popOutOrder.setBatchCode(erpOutOrderDto.getFLot());
            popOutOrder.setBatchNum(erpOutOrderDto.getFEntryQty().intValue());
            popOutOrder.setBatchNumDecimal(erpOutOrderDto.getFEntryQty());
            popOutOrder.setBatchValidDate(DateUtils.convertStringToDate(erpOutOrderDto.getFExpiryDate()));
            popOutOrder.setProductionDate(DateUtils.convertStringToDate(erpOutOrderDto.getFProductDate()));
            popOutOrder.setBatchPrice(erpOutOrderDto.getF_RPIH_HighPrice());
            popOutOrderDtos.add(popOutOrder);

        }
        return popOutOrderDtos;
    }

    public static List<PopErpSkuPriceDto> erpToPopPriceConvert(List<ErpSkuInfoDto> erpSkuInfoDtos) {
        List<PopErpSkuPriceDto> popErpSkuPriceDtos = new ArrayList<>();
        for (ErpSkuInfoDto erpSkuInfoDto : erpSkuInfoDtos) {
            if (erpSkuInfoDto.getF_RECN_SCJ().compareTo(BigDecimal.ZERO) == 0) {
                System.out.println("商品编码" + erpSkuInfoDto.getFNumber() + "成本价为0" + JSON.toJSONString(erpSkuInfoDto));
                continue;
            }
            PopErpSkuPriceDto popErpSkuPriceDto = new PopErpSkuPriceDto();
            popErpSkuPriceDto.setErpCode(erpSkuInfoDto.getFNumber());
            popErpSkuPriceDto.setChainPrice(erpSkuInfoDto.getF_RECN_SCJ());
            popErpSkuPriceDto.setBasePrice(erpSkuInfoDto.getF_RECN_SCJ());
            popErpSkuPriceDto.setMonomerPrice(erpSkuInfoDto.getF_RECN_SCJ());
            popErpSkuPriceDtos.add(popErpSkuPriceDto);
        }
        return popErpSkuPriceDtos;
    }
}
